﻿using System;
using System.IO;
using System.Xml;
using JTG.ServiceInterface.Attribute;
using JTG.ServiceInterface.HisResult;
using JTG.ServiceInterface.Model;
using Tzw.Common;

namespace JTG.ServiceInterface.InterfaceService
{
    /// <summary>
    /// 签到服务实现
    /// </summary>
    [FunctionName(FunctionType.隆昌妇幼保健院签到机)]
    public class CheckInService : ICheckInService
    {
        string _apiUrl = File.ReadAllText("ApiFile\\lcfbyApiUrl.xml");
        string _mainXml = File.ReadAllText("ApiFile\\lcfbyMain.xml");

        /// <summary>
        /// 签到
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="type"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public SignInfo GetRegisterInfo(string cardId, CheckInType type)
        {
            //加载接口文件
            var iXml = string.Format(File.ReadAllText("ApiFile\\lcfbyOutPatientRegQuery.xml"),
                Guid.NewGuid(), (int) type, cardId, $"{DateTime.Now:yyyy/MM/dd}", $"{DateTime.Now.AddDays(1):yyyy/MM/dd}");

            //加载主文件并拼装
            var mainXml = string.Format(_mainXml, iXml);

            File.WriteAllText("GetRegisterInfo.txt", mainXml);

            var apiResult = SoapHelper.GetSoapReSource(_apiUrl, mainXml);

            var respXml = XmlHelper.DeSerializer<Envelope>(apiResult);

            if (!respXml.Body.HIPMessageServerResponse.payload.res.message_cont.acknowledgement.typeCode.Equals("AA",
                StringComparison.OrdinalIgnoreCase))
            {
                //查询病人信息失败
                if (string.IsNullOrEmpty(respXml.Body.HIPMessageServerResponse.payload.res.message_cont.acknowledgement.Text[0].Trim()))
                {
                    return new SignInfo
                    {
                        ErrorMsg = apiResult
                    };
                }
                //查询病人信息失败
                return new SignInfo
                {
                    ErrorMsg = respXml.Body.HIPMessageServerResponse.payload.res.message_cont.acknowledgement.Text[0]
                };
                //throw new Exception("查询病人信息异常：" + respXml.Body.HIPMessageServerResponse.payload.res.message_cont.acknowledgement.Text);
            }

            //查询病人信息成功
            var patient = respXml.Body.HIPMessageServerResponse.payload.res.message_cont.acknowledgement
                .acknowledgementDetail.item;

            return new SignInfo
            {
                PatientId = patient.brid,
                SignId = patient.ghid,
                OrderNo = patient.no,
                OfficeName = patient.ks,
                DoctorName = patient.ys,
                PatientName = patient.xm,
                OfficePs = patient.zswz,
                SignNo = patient.hx,
                CardId = patient.jkkh,
            };
        }

        /// <summary>
        /// 查询挂号信息
        /// </summary>
        /// <returns></returns>
        public SignInfo CheckIn(SignInfo signInfo)
        {
            //加载接口文件
            var iXml = string.Format(File.ReadAllText("ApiFile\\lcfbyPatientsignin.xml"), Guid.NewGuid(), signInfo.SignId, signInfo.PatientId);

            //加载主文件并拼装
            var mainXml = string.Format(_mainXml, iXml);

            File.WriteAllText("CheckIn.txt", mainXml);//记录请求

            var apiResult = SoapHelper.GetSoapReSource(_apiUrl, mainXml);

            CheckInResponse.res respXml;
            try
            {
               respXml = XmlHelper.DeSerializer<CheckInResponse.Envelope>(apiResult).Body.HIPMessageServerResponse.payload.res;
            }
            catch
            {
                File.WriteAllText("CheckInError.txt", apiResult); //记录请求
                return new SignInfo
                {
                    ErrorMsg = apiResult
                };
            }

            if (!respXml.message_cont.acknowledgement.typeCode.Equals("AA",
                StringComparison.OrdinalIgnoreCase))
            {
                //查询病人信息失败
                return new SignInfo
                {
                    ErrorMsg = apiResult
                };
                //throw new Exception("查询病人信息异常：" + apiResult);
            }

            //查询病人信息成功
            var patient = respXml.message_cont.acknowledgement.acknowledgementDetail.text.OUTPUT;

            signInfo.QueueNo = patient.pdhm;
            signInfo.SignTime = patient.qdsj;
            signInfo.SingStatus = (SignStatus)patient.pdzt;
            signInfo.ErrorMsg = "";
            return signInfo;
        }
    }
}